﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace Gestion.Common.Extensions
{
    public static class ObjectExtension
    {
        [DebuggerStepThrough]
        public static IEnumerable<T> MakeEnumerable<T>(this T obj)
        {
            // ReSharper disable RedundantCast
            if ((object)obj != null)
                // ReSharper restore RedundantCast
                yield return obj;
            else
                // ReSharper disable RedundantJumpStatement
                yield break;
            // ReSharper restore RedundantJumpStatement
        }

        /// <summary>
        /// Get a DateTime object expressed as the Coordinated Universal Time (UTC).
        /// </summary>
        /// <param name="source">DateTime object</param>
        /// <returns>new <see cref="System.DateTime"/> expressed as the Coordinated Universal Time (UTC).</returns>
        public static DateTime ToUtc(this DateTime source)
        {
            return source.Kind == DateTimeKind.Unspecified
                ? DateTime.SpecifyKind(source, DateTimeKind.Utc)
                : (source.Kind == DateTimeKind.Local
                    ? source.ToUniversalTime()
                    : source /* DateTimeKind.Utc */ );
        }
    }
}
